package com.ruoyi.brs.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.bms.domain.BmsBooks;
import com.ruoyi.bms.domain.BmsUserUpload;
import com.ruoyi.bms.mapper.BmsBooksMapper;
import com.ruoyi.bms.mapper.BmsUserUploadMapper;
import com.ruoyi.brs.domain.BrsRentalOrder;
import com.ruoyi.brs.mapper.BrsRentalOrderMapper;
import com.ruoyi.brs.service.IBrsRentalOrderService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.brs.mapper.BrsUserMapper;
import com.ruoyi.brs.domain.BrsUser;
import com.ruoyi.brs.service.IBrsUserService;

/**
 * 管理租赁用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-12
 */
@Service
@Slf4j
public class BrsUserServiceImpl implements IBrsUserService
{
    @Autowired
    private BrsUserMapper brsUserMapper;
    @Autowired
    private BmsBooksMapper bmsBooksMapper;
    @Autowired
    private BrsRentalOrderMapper brsRentalOrderMapper;
    @Autowired
    private BmsUserUploadMapper brsUserUploadMapper;

    /**
     * 查询管理租赁用户信息
     * 
     * @param id 管理租赁用户信息主键
     * @return 管理租赁用户信息
     */
    @Override
    public BrsUser selectBrsUserById(Long id)
    {
        return brsUserMapper.selectBrsUserById(id);
    }

    /**
     * 查询管理租赁用户信息列表
     * 
     * @param brsUser 管理租赁用户信息
     * @return 管理租赁用户信息
     */
    @Override
    public List<BrsUser> selectBrsUserList(BrsUser brsUser)
    {
        return brsUserMapper.selectBrsUserList(brsUser);
    }

    /**
     * 新增管理租赁用户信息
     * 
     * @param brsUser 管理租赁用户信息
     * @return 结果
     */
    @Override
    public int insertBrsUser(BrsUser brsUser)
    {
        BrsUser brsUser1 = brsUserMapper.selectBrsUserByUserId(brsUser.getUserId());
        if(brsUser1 != null) return -1;
        brsUser.setCreateTime(DateUtils.getNowDate());
        return brsUserMapper.insertBrsUser(brsUser);
    }

    /**
     * 修改管理租赁用户信息
     * 
     * @param brsUser 管理租赁用户信息
     * @return 结果
     */
    @Override
    public int updateBrsUser(BrsUser brsUser)
    {
        brsUser.setUpdateTime(DateUtils.getNowDate());
        return brsUserMapper.updateBrsUser(brsUser);
    }

    /**
     * 批量删除管理租赁用户信息
     * 
     * @param ids 需要删除的管理租赁用户信息主键
     * @return 结果
     */
    @Override
    public int deleteBrsUserByIds(Long[] ids)
    {
        return brsUserMapper.deleteBrsUserByIds(ids);
    }

    /**
     * 删除管理租赁用户信息信息
     * 
     * @param id 管理租赁用户信息主键
     * @return 结果
     */
    @Override
    public int deleteBrsUserById(Long id)
    {
        return brsUserMapper.deleteBrsUserById(id);
    }


    /**
     * 用户租借书籍
     * @param bookId
     * @return
     */
    @Override
    public AjaxResult Userlease(Long bookId) {


        Long userId = SecurityUtils.getUserId();
        List<BrsRentalOrder> brsRentalOrders = brsRentalOrderMapper.selectBrsRentalOrderByUserId(userId);
        List<Long> collect = brsRentalOrders.stream().map(BrsRentalOrder::getBookId).collect(Collectors.toList());
        if(collect.contains(bookId)) return AjaxResult.error("请勿重复借阅");

        BrsUser brsUser = brsUserMapper.selectBrsUserByUserId(userId);

        if(brsUser.getUserPoints() < 3 ) return AjaxResult.error("积分不足");
        brsUser.setUserPoints(brsUser.getUserPoints() - 3);
        brsUser.setUserFrozenPoints(brsUser.getUserFrozenPoints() + 3);
        brsUserMapper.updateBrsUser(brsUser);

       BrsRentalOrder brsRentalOrder = BrsRentalOrder.builder()
                .userId(userId)
                .bookId(bookId)
                .rentalPoints(3L)
                .rentalStatus(1L)
                .rentalTime(DateUtils.getNowDate())
                .build();
        brsRentalOrderMapper.insertBrsRentalOrder(brsRentalOrder);
        return AjaxResult.success("借阅成功");
    }

    @Override
    public int insertfBrsUser() {
        if(brsUserMapper.selectBrsUserByUserId(SecurityUtils.getUserId()) != null) return 1;
        BrsUser brsUser = new BrsUser();
        brsUser.setUserId(SecurityUtils.getUserId());
        brsUser.setUserPoints(0L);
        brsUser.setUserFrozenPoints(0L);
        brsUser.setBalance(BigDecimal.valueOf(0));
        brsUser.setCreateTime(DateUtils.getNowDate());
        return  brsUserMapper.insertBrsUser(brsUser);
    }

    @Override
    public AjaxResult getLeaseBooksInfo(Long bookId) {
        Long userId = SecurityUtils.getUserId();
        List<BrsRentalOrder> brsRentalOrders =brsRentalOrderMapper.selectBrsRentalOrderByUserId(userId);
        List<Long> LeaseBooks = brsRentalOrders.stream().map(BrsRentalOrder::getBookId).collect(Collectors.toList());
        if(!LeaseBooks.contains(bookId)) return AjaxResult.error("请先借阅");
        return AjaxResult.success();
    }

    @Override
    public int updateBrsUserUpload(BmsUserUpload bmsUserUpload) {
        //查询之前的用户订单作对比
        BmsUserUpload bmsUserUpload1 = brsUserUploadMapper.selectBrsUserUploadById(bmsUserUpload.getId());
        if(bmsUserUpload.getStatus() == 1 && bmsUserUpload1.getStatus() == 1 ) return 0;
        //修改订单状态
        BmsBooks bmsBooks = bmsBooksMapper.selectBmsBooksById(bmsUserUpload1.getBookId());
        bmsUserUpload.setUpdateTime(DateUtils.getNowDate());
        bmsBooks.setBookStatus(bmsUserUpload.getStatus().toString());
        bmsBooksMapper.updateBmsBooks(bmsBooks);
        //修改积分
        if(bmsUserUpload.getStatus() == 1) upUserpoints(bmsUserUpload1.getUserId());
        return brsUserUploadMapper.updateBrsUserUpload(bmsUserUpload);
    }

    @Override
    public AjaxResult getUserInfo() {
        Long userId = SecurityUtils.getUserId();
        BrsUser brsUser = brsUserMapper.selectBrsUserByUserId(userId);
        return AjaxResult.success(brsUser);
    }

    public void upUserpoints(Long userId){
        BrsUser brsUser = brsUserMapper.selectBrsUserByUserId(userId);
        brsUser.setUserPoints(brsUser.getUserPoints() + 5);
        brsUserMapper.updateBrsUser(brsUser);
    }


}
